// ------------------------------------------------------------------------------------------------
// File   : DX9ShaderUtils.h 
// Date   : 2012-08-01
// Author : Weng xiao yi
// Descr. : 
// ------------------------------------------------------------------------------------------------

#ifndef OY_DX9SHADERUTILS_H
#define OY_DX9SHADERUTILS_H

#include <assert.h>
#include <d3dx9shader.h>
#include "../Config/D3D9Config.h"
#include "../../OyMath3D/OyMath3D.h"


// ------------------------------------------------------------------------------------------------
// MACROS
// ------------------------------------------------------------------------------------------------
// error handling
#define DX9Verify(a)		assert(a)

#define DX9_APPEND_DEFINE(_condition_, _definition_)    \
	if( (_condition_) != 0 )                            \
	{                                                   \
	strcat_s(_szFeatureDefinition, 4096, "#define ");	\
	strcat_s(_szFeatureDefinition, 4096, _definition_);	\
	strcat_s(_szFeatureDefinition, 4096, "\n");			\
	}

#define DX9_SAFE_RELEASE(p)			{ if(p) { (p)->Release(); (p)=NULL; } }
#define DX9_SAFE_DELETE(p)			{ if(p) { delete (p); (p)=NULL; } }

// ------------------------------------------------------------------------------------------------
// STRUCTURES
// ------------------------------------------------------------------------------------------------

// Transposed column-major matrix 
struct Float4x3
{
	OyVector4D    m_vColumns[3];
};

// Transposed column-major matrix 
struct Float4x2
{
	OyVector4D    m_vColumns[2];
};


//-------------------------------------------------------
// DX9ConvertToD3DMatrix
//-------------------------------------------------------
inline void DX9ConvertToD3DMatrix( OyMatrix & _to, const Float4x3 & _from )
{
	_to._11 = _from.m_vColumns[0].x;
	_to._21 = _from.m_vColumns[0].y;
	_to._31 = _from.m_vColumns[0].z;
	_to._41 = _from.m_vColumns[0].w;

	_to._12 = _from.m_vColumns[1].x;
	_to._22 = _from.m_vColumns[1].y;
	_to._32 = _from.m_vColumns[1].z;
	_to._42 = _from.m_vColumns[1].w;

	_to._13 = _from.m_vColumns[2].x;
	_to._23 = _from.m_vColumns[2].y;
	_to._33 = _from.m_vColumns[2].z;
	_to._43 = _from.m_vColumns[2].w;

	_to._14 = 0.0f;
	_to._24 = 0.0f;
	_to._34 = 0.0f;
	_to._44 = 1.0f;
}

//-------------------------------------------------------
// DX9ConvertToFloat4x3
//-------------------------------------------------------
inline void DX9ConvertToFloat4x3( Float4x3 & _to, const OyMatrix & _from )
{
	_to.m_vColumns[0].x = _from._11; 
	_to.m_vColumns[0].y = _from._21;
	_to.m_vColumns[0].z = _from._31; 
	_to.m_vColumns[0].w = _from._41;

	_to.m_vColumns[1].x = _from._12; 
	_to.m_vColumns[1].y = _from._22;
	_to.m_vColumns[1].z = _from._32; 
	_to.m_vColumns[1].w = _from._42;

	_to.m_vColumns[2].x = _from._13; 
	_to.m_vColumns[2].y = _from._23;
	_to.m_vColumns[2].z = _from._33; 
	_to.m_vColumns[2].w = _from._43;
}

//-------------------------------------------------------
// DX9ConvertToD3DMatrix
//-------------------------------------------------------
inline void DX9ConvertToD3DMatrix( OyMatrix & _to, const Float4x2 & _from )
{
	_to._11 = _from.m_vColumns[0].x;
	_to._21 = _from.m_vColumns[0].y;
	_to._31 = _from.m_vColumns[0].z;
	_to._41 = _from.m_vColumns[0].w;

	_to._12 = _from.m_vColumns[1].x;
	_to._22 = _from.m_vColumns[1].y;
	_to._32 = _from.m_vColumns[1].z;
	_to._42 = _from.m_vColumns[1].w;

	_to._13 = 0.0f;
	_to._23 = 0.0f;
	_to._33 = 1.0f;
	_to._43 = 0.0f;

	_to._14 = 0.0f;
	_to._24 = 0.0f;
	_to._34 = 0.0f;
	_to._44 = 1.0f;
}

//-------------------------------------------------------
// DX9ConvertToFloat4x2
//-------------------------------------------------------
inline void DX9ConvertToFloat4x2( Float4x2 & _to, const OyMatrix & _from )
{
	_to.m_vColumns[0].x = _from._11; 
	_to.m_vColumns[0].y = _from._21;
	_to.m_vColumns[0].z = _from._31; 
	_to.m_vColumns[0].w = _from._41;

	_to.m_vColumns[1].x = _from._12; 
	_to.m_vColumns[1].y = _from._22;
	_to.m_vColumns[1].z = _from._32; 
	_to.m_vColumns[1].w = _from._42;
}


#endif